<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>发布新书 - 商家端</title>
    <link href="https://fonts.googleapis.com/css?family=Roboto:400,700&display=swap" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
            font-family: 'Roboto', sans-serif;
            min-height: 100vh;
            margin: 0;
        }
        .layout {
            display: flex;
            min-height: 100vh;
        }
        .sidebar {
            width: 210px;
            background: #fff;
            box-shadow: 2px 0 16px rgba(0,0,0,0.07);
            display: flex;
            flex-direction: column;
            padding: 32px 0 0 0;
        }
        .sidebar-title {
            font-size: 1.3rem;
            font-weight: 700;
            color: #2d3a4b;
            text-align: center;
            margin-bottom: 36px;
        }
        .nav {
            display: flex;
            flex-direction: column;
            gap: 8px;
        }
        .nav a {
            display: block;
            padding: 12px 32px;
            color: #4a5a6a;
            text-decoration: none;
            font-size: 1.08rem;
            border-left: 4px solid transparent;
            transition: background 0.2s, color 0.2s, border-color 0.2s;
        }
        .nav a.active, .nav a:hover {
            background: #eaf3ff;
            color: #3478f6;
            border-left: 4px solid #7eb6ff;
        }
        .content {
            flex: 1;
            display: flex;
            align-items: flex-start;
            justify-content: center;
            padding: 48px 0;
        }
        .container {
            background: #fff;
            padding: 36px 32px 32px 32px;
            border-radius: 16px;
            box-shadow: 0 8px 32px rgba(0,0,0,0.15);
            width: 420px;
            max-width: 98vw;
        }
        .title {
            font-size: 1.7rem;
            font-weight: 700;
            color: #2d3a4b;
            margin-bottom: 24px;
            text-align: center;
        }
        .add-form {
            display: flex;
            flex-direction: column;
            gap: 18px;
        }
        .add-form label {
            font-weight: 500;
            color: #4a5a6a;
            margin-bottom: 4px;
            text-align: left;
        }
        .add-form input, .add-form textarea {
            padding: 10px 12px;
            border: 1px solid #d1d9e6;
            border-radius: 8px;
            font-size: 1rem;
            outline: none;
            transition: border 0.2s;
        }
        .add-form input:focus, .add-form textarea:focus {
            border-color: #7eb6ff;
        }
        .add-form textarea {
            resize: vertical;
            min-height: 60px;
            max-height: 180px;
        }
        .add-form input[type="file"] {
            padding: 0;
        }
        .submit-btn {
            background: linear-gradient(90deg, #7eb6ff 0%, #5eaefd 100%);
            color: #fff;
            font-weight: 700;
            border: none;
            border-radius: 8px;
            padding: 12px;
            font-size: 1.1rem;
            cursor: pointer;
            transition: background 0.2s;
        }
        .submit-btn:hover {
            background: linear-gradient(90deg, #5eaefd 0%, #7eb6ff 100%);
        }
        .logout-btn {
            margin: 32px 24px 0 24px;
            padding: 12px 0;
            width: calc(100% - 48px);
            background: linear-gradient(90deg, #ff6b6b 0%, #ffb36b 100%);
            color: #fff;
            font-weight: 700;
            border: none;
            border-radius: 8px;
            font-size: 1.08rem;
            cursor: pointer;
            transition: background 0.2s;
        }
        .logout-btn:hover {
            background: linear-gradient(90deg, #ffb36b 0%, #ff6b6b 100%);
        }
    </style>
</head>
<body>
    <div class="layout">
        <aside class="sidebar">
            <div class="sidebar-title">商家中心</div>
            <nav class="nav">
                <a href="merchant_home.html">首页</a>
                <a href="merchant_books.html">书籍管理</a>
                <a href="merchant_orders.html">订单处理</a>
                <a href="merchant_add_book.html" class="active">发布新书</a>
                <a href="merchant_profile.html">个人信息</a>
            </nav>
            <button id="logoutBtn" class="logout-btn">退出登录</button>
        </aside>
        <main class="content">
            <div class="container">
                <div class="title">发布新书</div>
                <form class="add-form" id="addBookForm">
                    <label for="bookName">书名</label>
                    <input type="text" id="bookName" name="bookName" required>

                    <label for="author">作者</label>
                    <input type="text" id="author" name="author" required>

                    <label for="price">价格（元）</label>
                    <input type="number" id="price" name="price" min="0" step="0.01" required>

                    <label for="stock">库存</label>
                    <input type="number" id="stock" name="stock" min="0" required>

                    <button type="submit" class="submit-btn">提交</button>
                </form>
            </div>
        </main>
    </div>
    <script>
    // 登录校验
    (function() {
        var userInfo = localStorage.getItem('userInfo');
        if (!userInfo) {
            window.location.href = 'login.html';
        }
    })();
    import("./api_config.js").then(({ API_BASE_URL }) => {
        document.getElementById('addBookForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            // 基础校验
            var title = document.getElementById('bookName').value.trim();
            var author = document.getElementById('author').value.trim();
            var price = parseFloat(document.getElementById('price').value);
            var stock = parseInt(document.getElementById('stock').value);
            if (!title) {
                alert('书名不能为空'); return;
            }
            if (!author) {
                alert('作者不能为空'); return;
            }
            if (isNaN(price) || price <= 0) {
                alert('价格必须为正数'); return;
            }
            if (isNaN(stock) || stock < 0) {
                alert('库存必须为非负整数'); return;
            }
            // 获取商家ID
            var userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
            var merchantId = userInfo.userId;
            if (!merchantId) {
                alert('未检测到商家身份，请重新登录');
                window.location.href = 'login.html';
                return;
            }
            // 组装参数
            var data = {
                merchantId: merchantId,
                title: title,
                author: author,
                price: price,
                stock: stock,
                status: 0
            };
            try {
                const response = await fetch(`${API_BASE_URL}/book`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data)
                });
                const result = await response.json();
                if (result.success) {
                    alert('新增书籍成功！');
                    document.getElementById('addBookForm').reset();
                    // 可跳转到书籍管理页
                    // window.location.href = 'merchant_books.html';
                } else {
                    alert(result.message || '新增书籍失败');
                }
            } catch (err) {
                alert('网络错误或服务器无响应');
            }
        });
    });
        document.getElementById('logoutBtn').onclick = function() {
            localStorage.removeItem('userInfo');
            window.location.href = 'login.html';
        };
    </script>
</body>
</html> 